import http from './http';

let ajaxs = {
    baseUrl: http.defaults.baseURL,
    httpImgUrl: http.defaults.httpImgUrl,
    hasToken: http.defaults.hasToken,
    get: function (url, data, success, fail) {
        http.get(url,  {
            params: data
        }).then( (res)=> {
            success(res);
        }).catch ( (error)=> {
            if (fail) fail(error);
        });
    },
    post: function (url, data, success, fail) {
        http.post(url, data).then( (res)=> {
            success(res);
        }).catch ( (error)=> {
            if (fail) fail(error);
        });
    },
    put: function (url, data, success, fail) {
        http.put(url, data).then( (res)=> {
            success(res);
        }).catch ( (error)=> {
            if (fail) fail(error);
        });
    },
    // 达观数据提交
    jsonpDG: function (type, url, data, success, fail) {
        let _getData = '';
        let _postData = '';
        if(type === 'get') {
            _getData = data;
        }
        else if(type === 'post') {
            _postData = data;
        }

        http({
            // 设置跨域请求头
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/json; charset=UTF-8',
                'Access-Control-Allow-Origin': '*'
            },
            // 请求方式
            method: type,
            url: url,
            params: _getData,
            data: _postData,
            dataType: "jsonp",
        }).then( (res) => {
            success(res);
        }).catch ( (error) => {
            if (fail) fail(error);
        });

    },

    // 跨域请求
    jsonp: function (type, url, data, success, fail) {
        let _getData = '';
        let _postData = '';
        if(type === 'get') {
            _getData = data;
        }
        else if(type === 'post') {
            _postData = data;
        }

        http({
            // 设置跨域请求头
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/json; charset=UTF-8',
                'Access-Control-Allow-Origin': '*'
            },
            // 请求方式
            method: type,
            url: url,
            params: _getData,
            data: _postData,
            dataType: "jsonp",
            // jsonp: "callback",//该参数不能修改
            // jsonpCallback: "successCallback",//回调函数名称，可以自定义
        }).then( (res) => {
            success(res);
        }).catch ( (error) => {
            if (fail) fail(error);
        });

    },
}
export default ajaxs;